package com.ctw.mall.service.impl;

import com.ctw.mall.dao.CategoryMapper;
import com.ctw.mall.pojo.Category;
import com.ctw.mall.service.ICategoryService;
import com.ctw.mall.vo.CategoryVo;
import com.ctw.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.ctw.mall.consts.MallConst.ROOT_PARENT_ID;

@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有的类目
     * @return
     */
    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {

        List<Category> categories= categoryMapper.selectAll();

        //查出parent_id=0

//        for(Category category : categories){
//            if(category.getParentId().equals(ROOT_PARENT_ID)){
//                CategoryVo categoryVo= new CategoryVo();
//                BeanUtils.copyProperties(category,categoryVo);
//                categoryVoList.add(categoryVo);
//            }
//        }

        //lambda表达式,e(自定义)就是category对象
        //lambda + stream,当处理有较多业务逻辑时，不适用
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e ->e.getParentId().equals(ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());

        //查询子目录
        findSubCategory(categoryVoList,categories);

        return ResponseVo.success(categoryVoList);
    }


    /**
     * 子类目
     * @param categoryVoList
     * @param categories
     */
    private void findSubCategory(List<CategoryVo> categoryVoList,List<Category> categories){

        for(CategoryVo categoryVo : categoryVoList){
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
            for(Category category : categories){
                //若查到，设置子目录,并继续查找
                if(categoryVo.getId().equals(category.getParentId())){
                     CategoryVo subCategoryVo= category2CategoryVo(category);
                    subCategoryVoList.add(subCategoryVo);
                }

                //排序,子目录
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());


                categoryVo.setSubCategories(subCategoryVoList);

                //递归,结果作为参数
                findSubCategory(subCategoryVoList,categories);
            }


        }

    }




    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> categories= categoryMapper.selectAll();
        findSubCategoryId(id,resultSet,categories);
    }


    private void findSubCategoryId(Integer id, Set<Integer> resultSet,List<Category> categories) {
        for(Category category: categories){
            if(category.getParentId().equals(id)){
                resultSet.add(category.getId());

                //递归
                findSubCategoryId(category.getId(),resultSet,categories);
            }
        }
    }


//lambda表达式转化,对象赋值
    private CategoryVo category2CategoryVo(Category category){
        CategoryVo categoryVo= new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }
}
